import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class pubMedConverter_analysis extends pubMedConverter_cleaning{
	
	boolean useNonTagged=false;

	Hashtable<String,Integer> authors; //author name -> unique number
	Hashtable<String,Integer> journals; //conference name -> unique number
	Hashtable<Integer,String> papers; //paper name/title -> unique number (use the unique ID, PIM)
	Hashtable<String,Integer> terms; //potential term -> unique number
	
	Hashtable<Integer, ArrayList<Integer>> paper_authors; //unique number -> author number
	Hashtable<Integer, ArrayList<Integer>> paper_journal; //unique number -> journal number
	Hashtable<Integer, ArrayList<Integer>> paper_terms; //unique number -> term number
	
	ArrayList<Integer> pims;
	
	int authorNumber = 0;
	int journalNumber = 0;
	int termNumber = 0;
	
	public pubMedConverter_analysis(boolean useNonTagged) {

		authors = new Hashtable<String,Integer>();
		journals = new Hashtable<String,Integer>();
		papers = new Hashtable<Integer,String>();
		terms = new Hashtable<String,Integer>();
		
		paper_authors= new Hashtable<Integer, ArrayList<Integer>>();
		paper_journal= new Hashtable<Integer, ArrayList<Integer>>();
		paper_terms= new Hashtable<Integer, ArrayList<Integer>>();
		
		pims  = new ArrayList<Integer>();
		
		this.useNonTagged = useNonTagged;
	}
	public ArrayList<Integer> convertList(Hashtable<String,Integer> lookup,ArrayList<String>values){
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		for(String s:values){
			list.add(lookup.get(s));
		}
		
		return list;
	}
	
	protected void start_1_onFile(File file){
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//parse using builder to get DOM representation of the XML file
			org.w3c.dom.Document dom = db.parse(file);
			
			
			Element docEle = dom.getDocumentElement();
			NodeList nl = docEle.getElementsByTagName("PubmedArticle");
			
			if(nl != null && nl.getLength() > 0) {
				for(int i = 0 ; i < nl.getLength();i++) {

					//get the Article element
					Element el = (Element)nl.item(i);
					
					NodeList mesh_nl = el.getElementsByTagName("MeshHeadingList");
					
					if(mesh_nl != null && mesh_nl.getLength() > 0) {
						parseElement(el);
					}else{
						//System.out.println("no mesh");
						if(useNonTagged)
							parseElement(el);
						
					}
					
				}
			}
			
			
			System.out.println("\tFinished Reading and Parsing");


		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	private void parseElement(Element el){
		
		
		String title = findMe_title(el);
		Integer pim = findMe_pim(el);
		ArrayList<String> aut = findMe_authors(el);
		String journal = findMe_journal(el);
		ArrayList<String> j = new ArrayList<String>();
		j.add(journal);
		ArrayList<String> mesh = findMe_mesh(el);

		if(!pims.contains(pim)){
			
			pims.add(pim);
			//papers.put(title, pim);
			papers.put(pim,title);
			
			if(!journals.containsKey(journal))
				journals.put(journal, new Integer(journalNumber++));
			
			for(String s:aut){
				if(!authors.containsKey(s))
					authors.put(s, new Integer(authorNumber++));
			}
			
			for(String s:mesh){
				if(!terms.containsKey(s))
					terms.put(s, new Integer(termNumber++));
			}
			
			paper_authors.put(pim, convertList(authors,aut));
			paper_journal.put(pim, convertList(journals,j));
			paper_terms.put(pim, convertList(terms,mesh));
			
		}	
	}

}
